import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './assets/reset.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import { JSEncrypt } from 'jsencrypt'

import scroll from 'vue-seamless-scroll'
Vue.use(scroll)

import http from './http.js'
Vue.prototype.http = http

//复制到粘贴板插件
import VueClipboard from 'vue-clipboard2'
VueClipboard.config.autoSetContainer = true
Vue.use(VueClipboard)

Vue.config.productionTip = false

// RSA加密
Vue.prototype.$getCode = (params) => {
    return new Promise((resolve) => {
        let encrypt = new JSEncrypt()
        encrypt.setPrivateKey('-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvF5p84FCcNfAD9VmQ93IZa9XnyWtYmGor5CVA3MKr4+soQ1W1AxQ7WAS3xabalGKSpbHV4r/Vc+gLbUrUE1LnrTxQInq1BWgQkxlmSa4VJx5SYIfyTnSp73WkFWYYgtR7TP+4yelVeCmTCOh5xXOe30Rs67IX2wt1BSWOPNdg6TnddUpmnZ0so3mZ/kkUEIAkGlPVfHVZhzckuGB9suibf/PsJBVy34TqbLxRy1JWj4W08YeCcXnS9UD1wmKnh1v5OOLnc+AvhUz+hpfZ23B7PVrO519CMftXVgdeBD0CdA+dlllmrN7IGRVgxqgcO2pBXrCnkDEKtt5XTnxKtuF1IS0vzlNPGkcEherPkCxV+d/p0JMU/yBTHLkKr2oRPdqAfPnVOKW2k8tAoQPRkEVQ1janzntRC6V2sSmsqu1OWMW6GGlbopIvXwKGr31JRtUzbnrp0Pt8sIXMYTTFfJILi3A7OFm5gkVGJ+TGe0GbH63v2DsiN1k9W53wqbBbl3LWf1oOqmihaS1XLUvlOY4YUBz2QmH8snsjeGhdU+rMMfSmBxermjpWvICyOZm/aO9XETLCAm3n8/SGQ8F63sTaVijlwICmkPMr4qu5lctNJrVXmpDSjMVCLqi71qliBRu/7YwvAbOt+05ceWgjQurtgXCuuE3SMRGDF/scnE+jyMCAwEAAQ==-----END PUBLIC KEY-----')
        let data = encrypt.encrypt(params)
        //console.log(params)
        resolve(data)
    })
}



router.afterEach((to) => {
    /* 路由发生变化修改页面 title */
    if (to.meta.title) {
        document.title = to.meta.title
    } else {
        document.title = '全能猫'
    }
    // next()
})


router.beforeEach((to, from, next) => {
    if (to.matched.length === 0) {
        from.path ? next({ path: from.path }) : next('/')
    } else {
        if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
            if (sessionStorage.getItem('token')) { // 通过vuex state获取当前的token是否存在
                next()
            } else {
                next({
                    path: '/login'
                        // query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
        } else {
            next()
        }
    }
})


new Vue({
    router,
    render: h => h(App),
}).$mount('#app')
